﻿--[[
   @brief    cf helper desc
   @author   amadeusyu
   @version  1.0
   @date     2015-04-07
--]]

dirui_control = {

    -- 保留老功能数据字段，用于灰度策略
    last_seq = "";
    last_gameid = 0;
    callback = "";
    cur_uin = 0;
    game_id = 0;

    show_horz_zone_list = nil;
    kHorzZoneMaxRowCount = 3;

    init = function()
        ierd_tgp.tpf_ui.Require('dirui\\model.lua');
        ierd_tgp.tpf_ui.Require('dirui\\view.lua');
        comm_center.listen_broadcast("Msg_CloudGamingDirDepsReady", dirui_control.on_dir_deps_ready);
        base_utility.log("[dirui_control] init success.");
    end;
    
    on_login_success = function()
    end;

    get_is_open_by_native = function()
        local feature_mgr =
        base_utility.get_component_adapter("IFeature_mgr",
            "ierd_tgp.feature.GetFeatureMgrAdapter();");
        if feature_mgr ~= nil then
            return feature_mgr:support_feature(dirui_model_constant.FEATURE_ID);
        end
        return false;
    end;

    open_dirui = function(id, json)
        ierd_tgp.tpf_ui.Log("[dirui]open_dirui.");
        dirui_model.is_open_by_native = dirui_control.get_is_open_by_native();
        local info = base_utility.json_to_table(json);
        if info ~= nil and info.data.game_id ~= nil then
            local game_lib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            local support_zone_select = game_lib:is_zone_select_support(info.data.game_id);
            if support_zone_select then
                local dir_info = {};
                dir_info.game_id = info.data.game_id;
                dir_info.uin = info.data.uin;
                -- 判断灰度策略打开方式
                if dirui_model.is_open_by_native == true then
                    dirui_control.req_zone_dir_info(0, base_utility.table_to_json(dir_info));
                else
                    dirui_control.open_web_dirui_old(0, base_utility.table_to_json(dir_info));
                end;
                
            else
                game_lib:launch_game(info.data.game_id, 0, 0, 0, "", ierd_tgp.game_library.ZONE_TYPE_FORMAL, "");
            end;
        end;
    end;

    open_web_dirui = function(id, json)
        ierd_tgp.tpf_ui.Log("[dirui]open_web_dirui.");
        dirui_model.is_open_by_native = dirui_control.get_is_open_by_native();
        if dirui_model.is_open_by_native == true then
            local info = base_utility.json_to_table(json);
            if info ~= nil and info.game_id ~= nil then
                local dir_info = {};
                dir_info.game_id = info.game_id;
                dir_info.uin = info.uin;
                dirui_control.req_zone_dir_info(0, base_utility.table_to_json(dir_info));
                if dirui_control.is_web_game(dirui_model.cur_game_id) then
                    -- 调整面板大小
                    base_utility.log("is web game");
                end;
            end;
        else
            dirui_control.open_web_dirui_old(id, json);
        end;
        
    end;

    req_zone_dir_info = function (id, json)
        -- body
        ierd_tgp.tpf_ui.Log("[dirui]req_zone_dir_info");
        local info = base_utility.json_to_table(json);
        if info ~= nil and info.game_id ~= nil then
            -- 显示选区面板
            if dirui_model.cur_game_id ~= 0 and dirui_model.cur_game_id ~= info.game_id then
                dirui_view.is_reopen_by_new_game = true;
            else
                dirui_view.is_reopen_by_new_game = false;
            end;
            local game_name = "";
            local game_lib = base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if game_lib ~= nil then
                local game_info = ierd_tgp.game_library.GameDetailInfo:new();
                game_lib:get_game_detail_info(info.game_id, game_info);
                game_name = game_info.name;
                game_info:delete();
            end;
            dirui_view.show_dir_wnd(game_name);
            if info.uin and info.uin ~= 0 then
                dirui_model.cur_uin = info.uin;
            else
                dirui_model.cur_uin = login_model.current_uin;
            end;
            dirui_model.cur_game_id = info.game_id;

            local continue_request = true;
            local data_result = comm_center.call_service("Svr_PrepareCloudGamingDirDeps",
                                                         { game_id = info.game_id });
            if nil ~= data_result and false == data_result.is_ready then
                continue_request = false;
            end
            if not continue_request then
                return;
            end
            -- 请求大区列表
            if dirui_model.cur_game_id == dirui_model_constant.LOL_GAME_ID then
                -- 先请求OSS配置
                dirui_model.req_lol_oss_tips();
            else
                dirui_control.req_dir_by_game_id(dirui_model.cur_game_id);
            end;
            -- 这里进行数据上报
            dirui_control.qos_report("show");
            
        end;
    end;

    req_dir_by_game_id = function (game_id)
        if game_id == GAME_ID.GAME_ID_MC then
            dirui_control.get_dirui_node_info_danji(game_id);
        elseif dirui_control.is_web_game(game_id) then
            dirui_control.get_dir_node_info_webgame(game_id, 0);
        else
            dirui_control.get_dirui_node_info(game_id);
        end;
        dirui_model.last_game_id = game_id;
    end;

    get_dirui_node_info_danji = function (game_id)
        --获取单机游戏的dirInfo
        local game_servers = banner_model.get_game_servers(game_id);
        if game_servers == nil then
            return;
        end;
        local dirui_node_info = {};
        dirui_node_info.gameID = game_id;
        dirui_node_info.isp = 1;
        dirui_node_info.pickedServers = nil;
        dirui_node_info.pickedServersCount = 0;
        dirui_node_info.result = true;
        dirui_node_info.rootInx = 0;
        dirui_node_info.totalDepth = 2;  --单机游戏MC是两层结构
        dirui_node_info.historyCount = 0;
        dirui_node_info.historyLogin = {};
        local count = game_servers.server_count;
        if count > 2 then 
            dirui_node_info.historyCount = 3;
            dirui_node_info.historyLogin = {1, 2, 3};
        else
            dirui_node_info.historyCount = count;
            count = 0;
            while (count < game_servers.server_count) do
                table.insert(dirui_node_info.historyLogin, count + 1);
                count = count + 1;
            end;
        end;
        dirui_node_info.nodeInfoCount = count + 1;
        dirui_node_info.nodeInfoSet = {};
        count = 0;
        while (count < dirui_node_info.nodeInfoCount) do
            if count == 0 then
                table.insert(dirui_node_info.nodeInfoSet, {
                    childrenCount = game_servers.server_count;
                    childrenInx = {};
                    hotNode = false;
                    isp = 0;
                    newNode = false;
                    parentInx = 65535;
                    pickedNode = false;
                    serverID = 0;
                    status = 1;
                    title = "dir_root";
                    version = "all";
                });
            else 
                table.insert(dirui_node_info.nodeInfoSet, {
                    childrenCount = 0;
                    childrenInx = nil;
                    hotNode = false;
                    isp = 0;
                    newNode = false;
                    parentInx = 0;
                    pickedNode = false;
                    serverID = game_servers.servers[count - 1].id;
                    status = 1;
                    title = game_servers.servers[count - 1].name;
                    version = game_servers.servers[count - 1].version;
                });
                table.insert(dirui_node_info.nodeInfoSet[1].childrenInx, count);
            end;
            count = count + 1;
        end;
        dirui_model.dirui_node_info = dirui_node_info;
    end;

    get_dir_node_info_webgame = function (game_id, page_idx)
        if (game_id == nil or page_idx == nil) then
            ierd_tgp.tpf_ui.Log(
            "get_dirui_node_info_webgame para is nil");
            return;
        end;
        ierd_tgp.tpf_ui.Log(
        "get_dirui_node_info_webgame,game_id="..game_id.."page="..page_idx);
        local server_dir =
            base_utility.get_component_adapter("IServer_dir",
                "ierd_tgp.server_dir.GetServerDirAdpter();");
        if server_dir ~= nil then
            -- 1代表GET_DIRUI
            server_dir:query_dir_ui_node_info_set_webgame(1, game_id, page_idx);
        end;
    end;

    get_dirui_node_info = function (game_id)
        ierd_tgp.tpf_ui.Log("get_dirui_node_info,game_id="..game_id..";uin ="..dirui_model.cur_uin);
        local server_dir =
            base_utility.get_component_adapter("IServer_dir",
                "ierd_tgp.server_dir.GetServerDirAdpter();");
        -- server_dir:query_dir_ui_node_info_set(1, game_id);
        server_dir:query_dir_ui_node_info_set_with_uin(1, game_id, dirui_model.cur_uin);
    end;

    -- 响应函数，这里得到大区列表
    on_res_dirui_info = function (event_id, param)
        if param == nil then
            return;
        end;
        -- 根据灰度策略，判断是否使用web响应
        base_utility.log("[jadonsuntest]"..param);
        if dirui_model.is_open_by_native == false then
            dirui_control.send_dirui_info_to_js_old(event_id, param);
            return;
        end;

        -- 下面为native 响应
        local info = base_utility.json_to_table(param);
        info.gameID = tostring(info.gameID);
        dirui_model.dirui_node_info = info;
        -- 得到区服信息,开始解析区服信息
        dirui_model.parse_dir_info_list();
        -- 显示默认区分列表
        dirui_control.init_dir_info_ui();

        -- 开始请求ping值
        dirui_control.res_ping_value();
            dirui_control.req_ping_value();
        -- if dirui_model.cur_game_id == dirui_model_constant.MXD2_GAME_ID or dirui_model.cur_game_id == dirui_model_constant.MXD2_GAME_ID_TEST then
            
        -- end;

    end;

    -- 初始化区服列表的默认显示
    init_dir_info_ui = function()
        dirui_view.show_default_list_ui();
        dirui_view.show_loading(false);
        dirui_view.show_dir_panel(true);
        -- 这里更新滚动条位置
        dirui_view.set_scroll_container_height();
    end;

    -- 选区面板点击时，界面显示数据刷新 begin

    -- 选中节点,node_index:选中节点的下标;node_type:选中节点的类型,is_history_zone:是否点击的是历史zone or 推荐zone
    click_dir_node = function(node_index, node_type, is_history_zone)
        if node_index < 1 or node_index > #(dirui_model.all_dir_info_list) then
            base_utility.log("[dirui_control]click_dir_node, click node index is illegal");
        end;
        if node_type == "zone" then
            -- zone节点
            dirui_control.set_select_by_zone(node_index);
            -- 设置zone节点选中态
            if is_history_zone == true then
                dirui_control.update_ui_after_click();
            else
                dirui_view.update_select_status_zone();
            end;
            
        elseif node_type == "area" then
            -- 设置选中的节点
            dirui_control.set_select_by_area(node_index);
            -- 更新zone节点
            dirui_model.update_zone_list_info();
            dirui_view.update_zone_list_ui();
            -- 设置area节点选中态
            dirui_view.update_select_status_area();
        elseif node_type == "isp" then
            -- isp节点
            dirui_control.set_select_by_isp(node_index);
            -- 更新area节点
            dirui_model.update_area_list_info();
            dirui_view.update_area_list_ui();
            -- 更新zone节点
            dirui_model.update_zone_list_info();
            dirui_view.update_zone_list_ui();
            -- 更新isp状态
            dirui_view.update_select_status_isp();
        end;
        -- dirui_control.update_ui_after_click();
        -- 更新历史列表
        dirui_view.update_select_status_history();
        -- container大小
        dirui_view.set_scroll_container_height();

        -- 重新请求ping值
        dirui_control.req_ping_value();
    end;

    update_ui_after_click = function()
        -- isp
        dirui_model.update_isp_list_info();
        dirui_view.update_isp_list_ui();
        -- area
        dirui_model.update_area_list_info();
        dirui_view.update_area_list_ui();
        -- zone
        dirui_model.update_zone_list_info();
        dirui_view.update_zone_list_ui();
        -- container大小
        dirui_view.set_scroll_container_height();
    end;

    -- 判断zone,zone_index对应的大区在click_index下
    is_zone_in_clicktab = function(zone_index, click_index)
        if zone_index < 1 or zone_index > #(dirui_model.all_dir_info_list) then
            base_utility.log("[dirui_control]is_zone_in_clicktab, zone_index is illegal");
            return false;
        end;
        if dirui_model.dir_tree_height == 1 then
            -- 没有isp和area,则在当前tab
            return true;
        end;
        if dirui_model.dir_tree_height == 2 then
            -- 只有isp,没有area
            local is_in = false;
            if dirui_model.all_dir_info_list[zone_index].parentInx == (click_index - 1) then
                is_in = true;
            end;
            return is_in;
        else
            -- 既有isp,也有area
            local is_in = false;
            local area_index = dirui_model.all_dir_info_list[zone_index].parentInx + 1;
            if area_index == click_index then
                is_in = true;
                return is_in;
            end;
            if area_index < 1 or area_index > #(dirui_model.all_dir_info_list) then
                base_utility.log("[dirui_control]is_zone_in_clicktab, area_index is illegal");
                return is_in;
            end;
            if dirui_model.all_dir_info_list[area_index].parentInx == (click_index - 1) then
                is_in = true;
            end;
            return is_in;
        end;
    end;

    -- 判断area,area_index对应的大区在click_index下
    is_area_in_clicktab = function(area_index, click_index)
        if area_index < 1 or area_index > #(dirui_model.all_dir_info_list) then
            base_utility.log("[dirui_control]is_area_in_clicktab, area_index is illegal");
            return false;
        end;
        if dirui_model.dir_tree_height < 3 then
            return true;
        end;
        if dirui_model.all_dir_info_list[area_index].parentInx == (click_index - 1) then
            return true;
        end;
        return false;
    end;

    set_select_by_zone = function(node_index)
        if dirui_model.dir_tree_height == 1 then
            dirui_model.selected_zone_index = node_index;
            dirui_model.selected_area_index = 0;
            dirui_model.selected_isp_index = 0;
        elseif dirui_model.dir_tree_height == 2 then
            dirui_model.selected_zone_index = node_index;
            dirui_model.selected_area_index = 0;
            dirui_model.selected_isp_index = dirui_model.all_dir_info_list[node_index].parentInx + 1;
        else
            dirui_model.selected_zone_index = node_index;
            dirui_model.selected_area_index = dirui_model.all_dir_info_list[node_index].parentInx + 1;
            dirui_model.selected_isp_index = dirui_model.all_dir_info_list[dirui_model.selected_area_index].parentInx + 1;
        end;
    end;

    set_select_by_area = function(node_index)
        dirui_model.selected_area_index = node_index;
        dirui_model.selected_isp_index = dirui_model.all_dir_info_list[node_index].parentInx + 1;
        -- 设置要选择的大区
        if dirui_control.is_zone_in_clicktab(dirui_model.selected_zone_index, node_index) == false then
            dirui_model.selected_zone_index = dirui_model.all_dir_info_list[node_index].childrenInx[1] + 1;
        end;
    end;


    set_select_by_isp = function(node_index)
        dirui_model.selected_isp_index = node_index;
        -- 设置选中的area节点
        if dirui_control.is_area_in_clicktab(dirui_model.selected_area_index, node_index) == false then
            -- 设置点击isp默认选中的area
            if dirui_model.dir_tree_height == 3 then
                dirui_model.selected_area_index = dirui_model.all_dir_info_list[node_index].childrenInx[1] + 1;
            else
                dirui_model.selected_area_index = 0;
            end;
        end;

        -- 设置选中的zone节点
        if dirui_control.is_zone_in_clicktab(dirui_model.selected_zone_index, node_index) == false then
            -- 设置点击isp默认选中的area
            if dirui_model.dir_tree_height == 2 then
                dirui_model.selected_zone_index = dirui_model.all_dir_info_list[node_index].childrenInx[1] + 1;
            elseif dirui_model.dir_tree_height == 3 then
                dirui_model.selected_zone_index = dirui_model.all_dir_info_list[dirui_model.selected_area_index].childrenInx[1] + 1;
            end;
        end;
    end;

    -- 选区面板点击时，界面显示数据刷新 end
    -- 界面事件处理  begin
    on_click_common_item = function(uilib, dw_click_id)
        -- 查找点击的类型
        local parent_id = ui_utility.get_ctrl_parent_id(dw_click_id);
        local click_type = "zone";
        local click_server_id = 0;
        local click_node_index = 0;
        local is_history_zone = false;
        for i = 1 , #(dirui_model.area_list_toshow) do
            if dirui_model.area_list_toshow[i].item_id == parent_id then
                click_type = "area";
                click_node_index = dirui_model.area_list_toshow[i].index;
                break;
            end;
        end;
        if click_node_index == 0 then
            -- 查找点击的是否是zone
            for i = 1 , #(dirui_model.zone_list_toshow) do
                if dirui_model.zone_list_toshow[i].item_id == parent_id then
                    click_type = "zone";
                    click_node_index = dirui_model.zone_list_toshow[i].index;
                    click_server_id = dirui_model.zone_list_toshow[i].zone_id;
                    break;
                end;
            end;
            -- 判断点击的是否是历史列表
            if click_node_index == 0 then
                for i = 1 , #(dirui_model.h_info_list) do
                    if dirui_model.h_info_list[i].item_id == parent_id then
                        click_type = "zone";
                        click_node_index = dirui_model.h_info_list[i].index;
                        click_server_id = dirui_model.h_info_list[i].zone_id;
                        is_history_zone = true;
                        break;
                    end;
                end;
                if is_history_zone == false then
                    for i = 1 , #(dirui_model.picked_info_list) do
                        if dirui_model.picked_info_list[i].item_id == parent_id then
                            click_type = "zone";
                            click_node_index = dirui_model.picked_info_list[i].index;
                            click_server_id = dirui_model.picked_info_list[i].zone_id;
                            is_history_zone = true;
                            break;
                        end;
                    end;
                end;
            end;
            
        end;
        -- 如果是lol，且不在白名单里面，则不响应
        if click_type == "zone" and
                click_server_id == dirui_model_constant.WHITELIST_ZONE_ID and
                dirui_model.cur_game_id == dirui_model_constant.LOL_GAME_ID then
            if dirui_model.white_is_in == false then
                return;
            end;
        end;
        if click_node_index > 0 then
            dirui_control.click_dir_node(click_node_index, click_type, is_history_zone);
        else
            base_utility.log("[dirui_control]on_click_common_item, failed to find click item");
        end;
    end;

    on_click_isp_item = function(uilib, dw_click_id)
        -- 选择isp
        local parent_id = ui_utility.get_ctrl_parent_id(dw_click_id);
        local click_node_index = 0;
        for i = 1, #(dirui_model.isp_list_toshow) do
            if dirui_model.isp_list_toshow[i].item_id == parent_id then
                click_node_index = dirui_model.isp_list_toshow[i].index;
                break;
            end;
        end;
        if click_node_index > 0 then
            dirui_control.click_dir_node(click_node_index, "isp", false);
        end;
        
    end;

    on_ok_click = function(uilib, dw_click_id)
        local select_zone = dirui_model.all_dir_info_list[dirui_model.selected_zone_index];
        if nil == select_zone then
            base_utility.log("[dirui_control]select_zone is nil, not response to ok click");
            return;
        end;
        if dirui_control.is_web_game(dirui_model.cur_game_id) then
            dirui_control.change_selected_server_webgame(dirui_model.cur_game_id,
            select_zone.serverID, "", select_zone.status);
        else
            dirui_control.change_selected_server(dirui_model.cur_game_id, select_zone.serverID, 
            select_zone.title);
        end;
        -- 数据上报
        dirui_control.qos_report("click", "ok_btn");
        dirui_view.un_init();
        dirui_control.close_window(CID_DIRUI_DLG);
    end;

    change_selected_server = function(game_id, server_id, title)
        ierd_tgp.tpf_ui.Log("change_selected_server,game_id="..game_id.."server_id="..server_id.. "uin="..dirui_model.cur_uin);
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local server_title = "";
        if title ~= nil then
            server_title = title;
        end;
        game_lib:change_server(game_id, server_id, dirui_model.cur_uin, server_title);
    end;
    
    change_selected_server_webgame = function(game_id, server_id, server_name, status)
        if game_id == nil or server_id == nil or server_name == nil or status == nil then
            ierd_tgp.tpf_ui.Log("change_selected_server_webgame para is null");
            return;
        end;
        ierd_tgp.tpf_ui.Log("change_selected_server_webgame,game_id="..game_id.."server_id="..server_id);
    end;

    on_cancel_click = function(uilib, dw_click_id)
        -- body
        dirui_view.un_init();
        dirui_control.close_window(CID_DIRUI_DLG);
    end;

    on_mouse_hover = function(uilib, dw_control_id)
        -- body
        if dirui_model.cur_game_id ~= dirui_model_constant.LOL_GAME_ID then
            return;
        end;
        local parent_id = ui_utility.get_ctrl_parent_id(dw_control_id);
        local hover_server_index = 0;
        -- 查找是否在zone列表中，如果不在，则不处理
        for i = 1, #(dirui_model.zone_list_toshow) do
            if dirui_model.zone_list_toshow[i].item_id == parent_id then
                if dirui_model.zone_list_toshow[i].zone_id == dirui_model_constant.WHITELIST_ZONE_ID then
                    hover_server_index = i;
                end;
                break;
            end;
        end;
        if hover_server_index == 0 then
            -- hover的不是WHITELIST_ZONE_ID
            return;
        end;
        -- 计算得到tips应该显示的位置
        dirui_view.cal_lock_tips_pos(dw_control_id, dirui_model.white_is_in);
    end;

    on_mouse_leave = function(uilib, dw_control_id)
        if dirui_model.cur_game_id ~= dirui_model_constant.LOL_GAME_ID then
            return;
        end;
        local parent_id = ui_utility.get_ctrl_parent_id(dw_control_id);
        local hover_server_index = 0;
        -- 查找是否在zone列表中，如果不在，则不处理
        for i = 1, #(dirui_model.zone_list_toshow) do
            if dirui_model.zone_list_toshow[i].item_id == parent_id then
                if dirui_model.zone_list_toshow[i].zone_id == dirui_model_constant.WHITELIST_ZONE_ID then
                    hover_server_index = i;
                end;
                break;
            end;
        end;
        if hover_server_index == 0 then
            -- hover的不是WHITELIST_ZONE_ID
            return;
        end;
        dirui_view.reset_lock_tip_pos(dw_control_id, dirui_model.white_is_in);
    end;
    -- 界面事件处理  end

        -- mxd2 ping值 begin
    req_ping_value = function()
        local req_params = {};
        req_params.game = dirui_model.cur_game_id;
        req_params.zone = {};
        for i = 1, #(dirui_model.zone_list_toshow) do
            table.insert(req_params.zone, dirui_model.zone_list_toshow[i].zone_id);
        end;
        -- 加上历史区服
        if #(dirui_model.h_info_list) > 0 then
            for i= 1, #(dirui_model.h_info_list) do
                if dirui_control.is_zone_id_in_table(dirui_model.h_info_list[i].zone_id, dirui_model.zone_list_toshow) == false then
                    table.insert(req_params.zone, dirui_model.h_info_list[i].zone_id);
                end;
            end;
        end;

        if #(dirui_model.picked_info_list) > 0 then
            for i= 1, #(dirui_model.picked_info_list) do
                if dirui_control.is_zone_id_in_table(dirui_model.picked_info_list[i].zone_id, dirui_model.zone_list_toshow) == false then
                    table.insert(req_params.zone, dirui_model.picked_info_list[i].zone_id);
                end;
            end;
        end;
        comm_center.call_service("Svr_GetGamePing", req_params);
    end;

    is_zone_id_in_table = function(key_value, content_table)
        for i = 1, #(content_table) do
            if content_table[i].zone_id == key_value then
                return true;
            end;
        end;
        return false;
    end;

    res_ping_value = function()
        comm_center.listen_broadcast("Msg_UpdatePing", "dirui_control.update_zone_ping_value");
    end;

    req_stop_ping_value = function()
        local req_params = {};
        req_params.game = dirui_model.cur_game_id;
        comm_center.call_service("Svr_StopPing", req_params);
        -- if dirui_model.cur_game_id == dirui_model_constant.MXD2_GAME_ID_TEST or dirui_model.cur_game_id == dirui_model_constant.MXD2_GAME_ID then
            
        -- end;
    end;
        -- 更新区服信息
    update_zone_ping_value = function(tbl_content)
        if tbl_content ~= nil then
            dirui_model.ping_value_list = tbl_content.ping;
            if #(dirui_model.ping_value_list) == 0 then
                return;
            end;
            -- 更新历史区服
            if #(dirui_model.h_info_list) > 0 then
                dirui_view.update_ping_value(dirui_model.h_info_list);
            elseif #(dirui_model.picked_info_list) > 0 then
                dirui_view.update_ping_value(dirui_model.picked_info_list);
            end;
            -- 更新zone列表
            if #(dirui_model.zone_list_toshow) > 0 then
                dirui_view.update_ping_value(dirui_model.zone_list_toshow);
            end;
        end;
    end;

    -- mxd2 ping值 end


    show_window = function(id)
        local component = ui_utility.get_ctrl_ptr_by_cid(id);
        if component ~= nil then
            component:Show(1);
            component:Restore();
            component:BringToTop();
        end;
    end;

    close_window = function(id)
        -- 抛出一个消息吧
        local   zParam = {};
        zParam["game_id"] = dirui_model.cur_game_id;
        zParam["uin"] = dirui_model.cur_uin;
        comm_center.send_broadcast("Msg_OnSelZoneClose", zParam);

        comm_center.send_broadcast("i_broadcast_dir_dlg_on_close", {});
        local uilib = ui_utility.uilib();
        local id = uilib:GetControlIDbyCustomID(id);
        layout_mgr.delay_remove_ctrl(id);
    end;
    
    on_close = function()
        dirui_view.un_init();
        dirui_control.req_stop_ping_value();
        dirui_control.close_window(CID_DIRUI_DLG);
    end;

    is_web_game = function(game_id)
        local is_web_game = false;
        return is_web_game;
    end;

    qos_report = function(type, position)
        base_utility.log("[dirui_control] qos report for tips");
        local report_data = ierd_tgp.tpf_ui.LuaQosDataEx:new();
        local pv_data = {};
        if nil ~= report_data then
            pv_data["game_id"] = dirui_model.cur_game_id;
            pv_data["uin"] = dirui_model.cur_uin;
            if position ~= nil then
                pv_data["position"] = position;
            end;
            report_data.oc_type    = 1;
            report_data.qos_kind = ierd_tgp.adapt_for_imports.qos.Qos_kind.kLocalGameVersion;
            report_data.str_action = "wegame_dir";
            report_data.str_type = type;
            report_data.str_json = base_utility.table_to_json(pv_data);
            base_utility.log("[dirui_control], qos, qos_kind="..report_data.qos_kind.."; str_action="..report_data.str_action.."; sub_action="..report_data.str_type.."; str_json="..report_data.str_json);
            base_utility.lua_qos_ex(report_data);
            report_data:delete();
        end;
    end;

    -- 以下为老代码，先保留，方便使用灰度策略
    on_js_callback = function(event_id, func_name, json)
        if json == nil or json == "" then
            return;
        end;
        local lua_table = base_utility.json_to_table(json);
        if lua_table == nil then
            return;
        end;
        if func_name == "get_dir_data" then
            if lua_table.gameid == GAME_ID.GAME_ID_MC then
                dirui_control.get_dirui_node_info_danji(lua_table.data.game_id);
            elseif dirui_control.is_web_game(lua_table.data.game_id) then
                dirui_control.get_dir_node_info_webgame(lua_table.data.game_id, lua_table.data.page_idx);
            else
                dirui_control.get_dirui_node_info(lua_table.data.game_id);
            end;
            dirui_control.callback = lua_table.callback_func;
            dirui_control.last_seq = lua_table.seq_num;
            dirui_control.last_gameid = lua_table.data.game_id;
        elseif func_name == "select_zone" then
            if dirui_control.is_web_game(lua_table.data.game_id) then
                dirui_control.change_selected_server_webgame(lua_table.data.game_id,
                lua_table.data.zone_id, lua_table.data.server_name, lua_table.data.status);
            else
                dirui_control.change_selected_server(lua_table.data.game_id, lua_table.data.zone_id);
            end;
            local complete_info = {};
            complete_info.seq_num = lua_table.seq_num;
            complete_info.retCode = 0;
            complete_info.msg = "ok";
            complete_info.src = "0";
            complete_info.data = lua_table.data;
            web_utility.lua_call_js_ex(lua_table.callback_func, base_utility.table_to_json(complete_info), CID_DIRUI_QBWEB);
        end;
    end;
    
    open_web_dirui_old = function(id, json)
        ierd_tgp.tpf_ui.Log("[dirui]open_web_dirui.");
        local info = base_utility.json_to_table(json);
        if info ~= nil and info.game_id ~= nil then
            dirui_control.cur_uin = info.uin;
            dirui_control.game_id = info.game_id;

            dirui_control.dw_view_id = layout_mgr.restore_layout("dirui.tpf");
            local dirui_url = 
            LoadStr("OPEN_DIRUI_URL_OLD")..info.game_id;
            if dirui_control.is_web_game(info.game_id) then
                -- 网页游戏选区高宽需要调整为500*750
                ui_utility.setproperty_by_cid(CID_DIRUI_DLG, "Width", 750);
                ui_utility.setproperty_by_cid(CID_DIRUI_DLG, "Height", 500);
                ui_utility.setproperty_by_cid(CID_DIRUI_QBWEB, "Width", 750);
                ui_utility.setproperty_by_cid(CID_DIRUI_QBWEB, "Height", 500);
                dirui_url =
                LoadStr("OPEN_DIRUI_URL_OLD")..info.game_id;
            end;
            dirui_control.navigate_old(CID_DIRUI_QBWEB, dirui_url);
            dirui_control.show_window_old(CID_DIRUI_DLG);
            -- 显示loading
            local p_loading = ui_utility.get_ctrl_ptr_by_path(
                dirui_control.dw_view_id, "pnlLoading");
            local qb_component = ui_utility.get_ctrl_ptr_by_cid(CID_DIRUI_QBWEB);
            if p_loading ~= nil then
                p_loading:Show(TRUE);
            else
                ierd_tgp.tpf_ui.Log("[dirui]get loading control failed");
            end;
            -- 隐藏浏览器组件
            if qb_component ~= nil then
                qb_component:Show(false);
            else
                ierd_tgp.tpf_ui.Log("[dirui]get loading control failed");
            end;
        end
    end;

    on_open_web_completed_old = function()
        -- 延迟到dir数据返回后显示，否则会出现黑闪
        --dirui_control.show_dir_ui();
    end;

    on_before_navigate_old = function()
        local qb_component = ui_utility.get_ctrl_ptr_by_cid(CID_DIRUI_QBWEB);
        if qb_component ~= nil then
            qb_component:Show(false);
        else
            ierd_tgp.tpf_ui.Log("[dirui]get loading control failed");
        end;
    end;
    
    send_dirui_info_to_js_old = function(event_id, param)
        if param == nil or dirui_control.callback == "" then
            return;
        end
        local info = base_utility.json_to_table(param);
        info.gameID = tostring(info.gameID);
        if info.gameID ~= dirui_control.last_gameid then
            return;
        end;
        local complete_info = {};
        complete_info.seq_num = dirui_control.last_seq;
        complete_info.retCode = 0;
        complete_info.msg = "ok";
        complete_info.src = "0";
        complete_info.data = info;
        web_utility.lua_call_js_ex(dirui_control.callback, base_utility.table_to_json(complete_info), CID_DIRUI_QBWEB);
        dirui_control.last_seq = "";
        dirui_control.callback = "";

        dirui_control.show_dir_ui_old();
    end;

    show_dir_ui_old = function()
        -- 隐藏loading
        local p_loading = ui_utility.get_ctrl_ptr_by_path(
            dirui_control.dw_view_id, "pnlLoading");
        if p_loading ~= nil then
            p_loading:Show(FALSE);
        else
            ierd_tgp.tpf_ui.Log("[dirui]get loading control failed");
        end;

        local qb_component = ui_utility.get_ctrl_ptr_by_cid(CID_DIRUI_QBWEB);
        if qb_component ~= nil then
            qb_component:Show(true);
        else
            ierd_tgp.tpf_ui.Log("[dirui]get loading control failed");
        end;
    end;

    navigate_old = function(browser_id, url)
        local component = ui_utility.get_ctrl_ptr_by_cid(browser_id);
        local const_ui_str, str_holder = U8toCUIStr(url);
        component:Navigate(const_ui_str);
    end;

    show_window_old = function(id)
        local component = ui_utility.get_ctrl_ptr_by_cid(id);
        if component ~= nil then
            component:Show(1);
            component:Restore();
            component:BringToTop();
        end
    end;

    should_show_horz_zone_list = function()
        if nil ~= dirui_control.show_horz_zone_list then
            return dirui_control.show_horz_zone_list;
        end

        dirui_control.show_horz_zone_list = false;
        local disable_horz_zone_ui = false;
        local cfg_tbl = nil;
        local data = {key = "client_dirui_config"};
        local cfg = comm_center.call_service("i_svr_get_common_cfg", data);
        if nil ~= cfg and nil ~= cfg.value and "" ~= cfg.value then
            cfg_tbl = base_utility.json_to_table(cfg.value);
            if nil ~= cfg_tbl then
                disable_horz_zone_ui = cfg_tbl.disable_horz_zone_ui or false;
            end
        end

        if disable_horz_zone_ui then
            return dirui_control.show_horz_zone_list;
        end

        local max_row = dirui_control.kHorzZoneMaxRowCount;
        if dirui_model.dir_tree_height == 2 then
            if #dirui_model.h_index_list > 0 or #dirui_model.picked_info_list > 0 then
                max_row = max_row - 1;
            end
            
            if #dirui_model.isp_list_toshow <= max_row then
                for i = 1, #dirui_model.isp_list_toshow do
                    local info_idx = dirui_model.isp_list_toshow[i].index or 0;
                    if info_idx > 0 and info_idx <= #dirui_model.all_dir_info_list then
                        local node_info = dirui_model.all_dir_info_list[info_idx];
                        if nil ~= node_info and nil ~= node_info.childrenCount then
                            if node_info.childrenCount > 4 then
                                dirui_control.show_horz_zone_list = false;
                                break;
                            else
                                dirui_control.show_horz_zone_list = true;
                            end
                        end
                    end
                end
            end
        end
        return dirui_control.show_horz_zone_list;
    end;

    on_dir_deps_ready = function(param)
        if nil == param or nil == param.game_id then
            ierd_tgp.tpf_ui.Log("[dirui]on_dir_deps_ready invalid param");
            return;
        end;
        if dirui_model.cur_game_id ~= param.game_id then
            return;
        end;
        -- 请求大区列表
        if dirui_model.cur_game_id == dirui_model_constant.LOL_GAME_ID then
            -- 先请求OSS配置
            dirui_model.req_lol_oss_tips();
        else
            dirui_control.req_dir_by_game_id(dirui_model.cur_game_id);
        end;
       -- 这里进行数据上报
       dirui_control.qos_report("show");
    end;

}


dirui_control.init();
